package com.wjzjq.hiring.talent.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wjzjq.hiring.common.Constant;
import com.wjzjq.hiring.common.util.AppUtil;
import com.wjzjq.hiring.talent.dao.ResumeDao;
import com.wjzjq.hiring.talent.entity.ResumeEntity;

/**
 * Servlet implementation class ResumeServlet
 */
@WebServlet("/ResumeServlet")
public class ResumeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ResumeDao resumeDao = new ResumeDao();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 解决乱码问题
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("deleteResume".equals(command)) {
			this.delete(request, response);
		} else if ("addResume".equals(command)) {
			this.addResume(request, response);
		} else if ("preUpdate".equals(command)) {
			this.preUpdate(request, response);
		} else if ("update".equals(command)) {
			this.update(request, response);
		} else if ("deleteByIds".equals(command)) {
			this.deleteByIds(request, response);
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("id"));
		ResumeEntity resume = new ResumeEntity();
		resume.setIntention(request.getParameter("intention"));
		resume.setProjectExperience(request.getParameter("projectExperience"));
		resume.setWorkExperience(request.getParameter("workExperience"));
		int row = ResumeDao.updateResume(id, resume);
		if (row != 0) {
			response.sendRedirect(request.getContextPath()
					+ "/TalentServlet?command=main");
			;
		}
	}

	private void preUpdate(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("resumeId"));
		ResumeEntity resume;
		try {
			resume = ResumeDao.getResume(id);
			// 输出 检查
			if (resume != null) {
				request.setAttribute("resume", resume);
				request.getRequestDispatcher("/view/talent/resume-update.jsp")
						.forward(request, response);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void addResume(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// int talentId = ((TalentEntity)
		// request.getSession().getAttribute("talent")).getId();//害老子白写
		int talentId = AppUtil.getTalentId(request);
		String intention = request.getParameter("intention");
		String workExperience = request.getParameter("workExperience");
		String projectExperience = request.getParameter("projectExperience");
		ResumeEntity resume = new ResumeEntity();
		// 输出 检查
		resume.setTalentId(talentId);
		resume.setIntention(intention);
		resume.setProjectExperience(projectExperience);
		resume.setWorkExperience(workExperience);

		int row = ResumeDao.addResume(resume);
		if (row != 0) {
			response.sendRedirect(request.getContextPath()
					+ "/TalentServlet?command=main&result=0");
		} else {
			// 失败回显信息
			request.setAttribute("resume", resume);
			request.setAttribute("result", Constant.OPERATE_ERROR);
			request.getRequestDispatcher("/view/talent/resume-add.jsp")
					.forward(request, response);

		}
	}

	/**
	 * 表格删除
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */

	private  void deleteByIds(HttpServletRequest request,
			HttpServletResponse response) {
		String deleteIdsStr = request.getParameter("resumeIds");
		String[] deleteIdsStrs = deleteIdsStr.split(",");
		int len = deleteIdsStrs.length;
		int[] deleteIds = new int[len];
		try {
			for (int i = 0; i < len; i++) {
				deleteIds[i] = Integer.valueOf(deleteIdsStrs[i]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (resumeDao.deleteByIds(deleteIds)) {
				response.sendRedirect(request.getContextPath()
						+ "/TalentServlet?command=main");
			} else {
				request.setAttribute("error", "删除失败");
				request.getRequestDispatcher("/view/talent/main.jsp").forward(
						request, response);
			}
		} catch (IOException | ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("resumeId"));
		int talentId = AppUtil.getTalentId(request);
		int row = resumeDao.delete(id);
		if (row == 1) {
			// TalentEntity t = (TalentEntity)
			// request.getSession().getAttribute("talent");
			// request.setAttribute("talent", t);
			request.setAttribute("resumeList",
					resumeDao.getResumeList(talentId));// 若删除
														// 则重置这个显示的集合
			request.getRequestDispatcher("/view/talent/main.jsp").forward(
					request, response);
		} else {
			request.getRequestDispatcher("/view/talent/main.jsp").forward(
					request, response);
		}
	}

}